﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using Remider.Entity;
using Remider.Utility;
using SQLiteHelper;
using System.Windows.Forms;

namespace Remider.Forms
{
    public partial class AppointForm : Form
    {
        internal Appointment Current
        {
            get;
            set;
        }

        public AppointForm()
        {
            InitializeComponent();
        }

        private void AppointForm_Load(object sender, EventArgs e)
        {
            BindAppointType();
            BindLoopMode();
            BindAppointAction();
            if (Current != null)
            {
                BindAppointment();
            }
        }

        private void BindAppointment()
        {
            TitleTextBox.Text = Current.Title;
            ContentTextBox.Text = Current.Content;
            dateTimePicker1.Value = Current.ScheduleDate;
            ActionComboBox.SelectedText = CacheManager.AppointActionType[Current.ActionType];
            AppointTypeComboBox.SelectedText = CacheManager.AppointmentType[Current.AppointmentType];
        }

        private void BindAppointAction()
        {
            foreach (var pair in CacheManager.AppointActionType)
            {
                ActionComboBox.Items.Add(new ComboBoxItem(pair.Value, pair.Key));
            }
            ActionComboBox.SelectedIndex = 0;
        }

        private void BindLoopMode()
        {
            foreach (var pair in CacheManager.LoopMode)
            {
                LoopModeComboBox.Items.Add(new ComboBoxItem(pair.Value, pair.Key));
            }
            LoopModeComboBox.SelectedIndex = 0;
        }

        private void BindAppointType()
        {
            foreach (var pair in CacheManager.AppointmentType)
            {
                AppointTypeComboBox.Items.Add(new ComboBoxItem(pair.Value, pair.Key));
            }
            AppointTypeComboBox.SelectedIndex = 0;
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (Current == null)
            {
                Current = new Appointment
                              {
                                  IsDone = "N",
                                  IsDelete = "N",
                                  LastRunDate = null,
                                  Title = TitleTextBox.Text,
                                  Content = ContentTextBox.Text,
                                  ScheduleDate = dateTimePicker1.Value,
                                  NextRunDate = dateTimePicker1.Value,
                                  AppointmentType = ((ComboBoxItem)AppointTypeComboBox.SelectedItem).Value,
                                  ActionType = ((ComboBoxItem)ActionComboBox.SelectedItem).Value,
                                  LoopInt = ((ComboBoxItem)LoopModeComboBox.SelectedItem).Value * int.Parse(LoopTextBox.Text)
                              };
                Current.Insert();
            }
            else
            {

            }
            Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

    }

    internal class ComboBoxItem
    {
        private string _text;
        private int _value;

        public string Text
        {
            get { return _text; }
        }

        public int Value
        {
            get { return _value; }
        }

        public ComboBoxItem(string text, int value)
        {
            this._text = text;
            this._value = value;
        }

        public override string ToString()
        {
            return _text;
        }
    }
}